import os
import sys
from ConfigParser import SafeConfigParser
import copy
import nose
from patchedXunit import patchedXunit

def deleteOldNoseid():
    files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith("noseid")]
    for f in files:
        os.remove(f)
        
if __name__ == '__main__':
    testFilter = False
    rerun = 0
    #nose.main(argv=['nosetests','-s','-v','--processes=10'])
    #nose.run(argv=['nosetests','-v', '--processes=10','--tc-file','config1.ini'], plugins=[MultiProcess(),TestConfig()])
    configFile = sys.argv[1]
    if len(sys.argv) >2:
        testsToRunconfigFile = sys.argv[2]
        testsParser = SafeConfigParser()
        testsParser.read(testsToRunconfigFile)
        testFilter = True
        
    parser = SafeConfigParser()
    parser.read(configFile)
    xunitFile = parser.get('TEST_CONFIG','xunitFile')
    
    #configuration file name must be unique since this name is used for 
    #file storing test ids. Each process must have unique file with test ids 
    #base_arguments = ['nosetests','-v','-s','--with-xunit',
    #               '--tc-file',configFile,
    #               '--id-file',configFile.replace('ini','noseid'),                 
    #               ]
    base_arguments = ['nosetests',
                      '-v',
                      '--with-yanc',
                      '-s','--with-xunit',
                      '--tc-file',configFile,
                      '--id-file',configFile.replace('ini','noseid'),                 
                   ]
        
    arguments = copy.deepcopy(base_arguments)
    if parser.has_option('TEST_CONFIG','rerun'):
        rerun = int(parser.get('TEST_CONFIG','rerun'))
        
    if testFilter and testsParser.has_option('TESTS_TO_RUN','modules'):
        testsParam = testsParser.get('TESTS_TO_RUN','modules')
        arguments.append("--tests="+testsParam)
    
    if testFilter and testsParser.has_option('TESTS_TO_RUN', 'exclude'):
        excludeParam = testsParser.get('TESTS_TO_RUN','exclude')
        arguments.append("--exclude="+excludeParam)
    
    if testFilter and testsParser.has_option('TESTS_TO_RUN', 'match'):
        includeParam = testsParser.get('TESTS_TO_RUN','match')
        arguments.append("--match="+includeParam)
        
    if testFilter and testsParser.has_option('TESTS_TO_RUN', 'attrib'):
        attrib = testsParser.get('TESTS_TO_RUN','attrib')
        arguments.append("-A")
        arguments.append(attrib)
    
    arguments.append('--xunit-file='+xunitFile)
    arguments.append('--with-id')

    tryNumber = 1
    
    deleteOldNoseid()
        
    while rerun+1 > 0:   
        nose.run(argv= arguments,addplugins = [patchedXunit(configFile)])
        
 #       shutil.copyfile('G:\\workspace\\nosetests-dummy\\src\\cfg_FF_ANY_5562.noseid', 
 #                       'G:\\workspace\\nosetests-dummy\\src\\cfg_FF_ANY_5562.noseid.'+str(tryNumber))
        
        arguments = copy.deepcopy(base_arguments)
        arguments.append('--failed')
    
        arguments.append('--xunit-file='+xunitFile.replace('xml', "relaunch_%d.xml"%tryNumber))
        rerun -=1
        tryNumber+=1 
    
 
    
